using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace MineFlagger {

	public class AIConfigControl : System.Windows.Forms.UserControl {

		private AIConfig _aiConfig;
		private ResultingConfoundsConfig _rcConfigForFPConfoundsExt;

		#region WinVars

		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tabPage1;
		private System.Windows.Forms.TabPage tabPage2;
		private Marksam.Controls.NumberBox numMaxBaseCellsDeep;
		private System.Windows.Forms.CheckBox chkMaxBaseCellsDeep;
		private Marksam.Controls.NumberBox numMaxPathsFromBaseCell;
		private Marksam.Controls.NumberBox numMaxNewBaseCells;
		private System.Windows.Forms.CheckBox chkMaxNewBaseCells;
		private System.Windows.Forms.CheckBox chkMaxPathsFromBaseCell;
		private System.Windows.Forms.TabPage tabPage4;
		private System.Windows.Forms.CheckBox chkShowSteps;
		private Marksam.Controls.NumberBox numShowStepsDelay;
		private System.Windows.Forms.CheckBox chkEnableSafeCell2;
		private MineFlagger.ConfoundsControl confoundsControl1;
		private System.Windows.Forms.CheckBox chkEnableSafeCell1;
		private System.Windows.Forms.CheckBox chkEnableFlagPath2;
		private System.Windows.Forms.CheckBox chkEnableFlagPath1;
		private System.Windows.Forms.CheckBox chkEnableConfoundsExt;
		private System.Windows.Forms.Button butConfoundsSettings;
		private System.Windows.Forms.CheckBox chkReportData;
		private System.Windows.Forms.CheckBox chkEnableProbability;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		#endregion

		public AIConfigControl() {
			InitializeComponent();
			
			_aiConfig = new AIConfig();
			setDefaults();
			setAIConfig();
		}

		public AIConfig AIConfig {
			get { return _aiConfig; }
			private set { _aiConfig = value; }
		}

		private void setDefaults() {
			
			// Genral
			chkEnableSafeCell1.Checked = true;
			chkEnableFlagPath1.Checked = true;
			chkEnableProbability.Checked = true;

			chkShowSteps.Checked = true;
			numShowStepsDelay.Text = "50";

			// Safe Cell
			// Defaults already set

			// Flag path
			chkEnableConfoundsExt.Checked = true;
			chkMaxBaseCellsDeep.Checked = true;
			chkMaxNewBaseCells.Checked = true;
			chkMaxPathsFromBaseCell.Checked = true;

			numMaxBaseCellsDeep.Text = "7";
			numMaxNewBaseCells.Text = "3";
			numMaxPathsFromBaseCell.Text = "3";

			// _rcConfigForFPConfoundsExt

			if (_rcConfigForFPConfoundsExt == null) {
				_rcConfigForFPConfoundsExt = new ResultingConfoundsConfig();
			}

			_rcConfigForFPConfoundsExt.SearchType = SearchType.Skimpy;
			_rcConfigForFPConfoundsExt.UseRecursively = true;
			_rcConfigForFPConfoundsExt.RecursionLimit = -1;
		}


		private void setAIConfig() {


			// Show steps and delay
			if (chkShowSteps.Checked && numShowStepsDelay.Text != "") {
				_aiConfig.ShowSteps	= true;
				_aiConfig.StepDelay = int.Parse(numShowStepsDelay.Text);
                if (_aiConfig.StepDelay < 0) _aiConfig.StepDelay = 0;
			} else {
				_aiConfig.ShowSteps	= false;
				_aiConfig.StepDelay = 0;
			}

			// Report data
			_aiConfig.ReportData = chkReportData.Checked;


			// ==Safe Cell==

			if (chkEnableSafeCell1.Checked) {
				if (_aiConfig.SafeCellAIConfig == null) {
					_aiConfig.SafeCellAIConfig = new SafeCellAIConfig(confoundsControl1.ResultingConfoundsConfig);
				} else {
					// Avoid creating a new object
					_aiConfig.SafeCellAIConfig.ResultingConfoundsConfig.SearchType = confoundsControl1.ResultingConfoundsConfig.SearchType;
					_aiConfig.SafeCellAIConfig.ResultingConfoundsConfig.UseRecursively = confoundsControl1.ResultingConfoundsConfig.UseRecursively;
					_aiConfig.SafeCellAIConfig.ResultingConfoundsConfig.RecursionLimit = confoundsControl1.ResultingConfoundsConfig.RecursionLimit;
				}
			} else {
				_aiConfig.SafeCellAIConfig = null;
			}

			// ==Flag Path==

			if (chkEnableFlagPath1.Checked) {
				if (_aiConfig.FlagPathAIConfig == null) {
					_aiConfig.FlagPathAIConfig = new FlagPathAIConfig();
				}

				_aiConfig.FlagPathAIConfig.UseSameFlagInAllPaths = true;
				_aiConfig.FlagPathAIConfig.UseCellNotInAnyPaths = false;

				if (_aiConfig.FlagPathAIConfig.FillBaseConfig == null) {
					_aiConfig.FlagPathAIConfig.FillBaseConfig = new FillBaseConfig();
				}
				
				// Resulting Confounds
				if (chkEnableConfoundsExt.Checked) {
					_aiConfig.FlagPathAIConfig.FillBaseConfig.UseResultingConfounds = true;
					_aiConfig.FlagPathAIConfig.FillBaseConfig.ResultingConfoundsConfig = _rcConfigForFPConfoundsExt;
					
				} else {
					_aiConfig.FlagPathAIConfig.FillBaseConfig.UseResultingConfounds = false;
				}
				
				// Depth
				if (chkMaxBaseCellsDeep.Checked && numMaxBaseCellsDeep.Text != "") {
					_aiConfig.FlagPathAIConfig.FillBaseConfig.Depth = int.Parse(numMaxBaseCellsDeep.Text);
				} else {
					_aiConfig.FlagPathAIConfig.FillBaseConfig.Depth = -1;
				}

				// MaxNewBaseCells
				if (chkMaxNewBaseCells.Checked && numMaxNewBaseCells.Text != "") {
					_aiConfig.FlagPathAIConfig.FillBaseConfig.MaxNewBaseCells = int.Parse(numMaxNewBaseCells.Text);
				} else {
					_aiConfig.FlagPathAIConfig.FillBaseConfig.MaxNewBaseCells = -1;
				}

				// MaxPaths
				if (chkMaxPathsFromBaseCell.Checked && numMaxPathsFromBaseCell.Text != "") {
					_aiConfig.FlagPathAIConfig.FillBaseConfig.MaxPaths = int.Parse(numMaxPathsFromBaseCell.Text);
				} else {
					_aiConfig.FlagPathAIConfig.FillBaseConfig.MaxPaths = -1;
				}

			} else {
				_aiConfig.FlagPathAIConfig = null;
			}


			// == Prob and EndOfGame ==
			_aiConfig.UseProbabilityAI = chkEnableProbability.Checked;
			_aiConfig.UseOneMineEndOfGameAI = true;


		}



		// New
		private void chkEnableSafeCell_CheckedChanged(object sender, System.EventArgs e) {
			bool enabled = ((CheckBox)sender).Checked;

			chkEnableSafeCell1.Checked = enabled;
			chkEnableSafeCell2.Checked = enabled;

			validateAll();
		}

		private void chkEnableFlagPath_CheckedChanged(object sender, System.EventArgs e) {
			bool enabled = ((CheckBox)sender).Checked;

			chkEnableFlagPath1.Checked = enabled;
			chkEnableFlagPath2.Checked = enabled;

			validateAll();
		}


		private void CheckedChanged(object sender, System.EventArgs e) {
			validateAll();
		}


		private void validateAll() {
			// ==Safe Cell==
			chkEnableSafeCell2.Checked = chkEnableSafeCell1.Checked;
			confoundsControl1.Enabled = chkEnableSafeCell1.Checked;


			// ==Flag Path==
			chkEnableFlagPath2.Checked = chkEnableFlagPath1.Checked;

			if (!chkEnableFlagPath1.Checked) {
				// Disable everything
				chkEnableConfoundsExt.Enabled = false;
				butConfoundsSettings.Enabled = false;
								
				chkMaxBaseCellsDeep.Enabled = false;
				chkMaxNewBaseCells.Enabled = false;
				chkMaxPathsFromBaseCell.Enabled = false;
				
				numMaxBaseCellsDeep.Enabled = false;
				numMaxNewBaseCells.Enabled = false;
				numMaxPathsFromBaseCell.Enabled = false;

			} else {
				// enable selectivly

				chkEnableConfoundsExt.Enabled = true;
				chkMaxBaseCellsDeep.Enabled = true;
				chkMaxNewBaseCells.Enabled = true;
				chkMaxPathsFromBaseCell.Enabled = true;

				butConfoundsSettings.Enabled = chkEnableConfoundsExt.Checked;

				numMaxBaseCellsDeep.Enabled = chkMaxBaseCellsDeep.Checked;
				numMaxNewBaseCells.Enabled = chkMaxNewBaseCells.Checked;
				numMaxPathsFromBaseCell.Enabled = chkMaxPathsFromBaseCell.Checked;
			}

			// Other
			numShowStepsDelay.Enabled = chkShowSteps.Checked;

			setAIConfig();

		}

	

		#region AutoGenUtil
		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if(components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
			this.chkEnableFlagPath2 = new System.Windows.Forms.CheckBox();
			this.numMaxBaseCellsDeep = new Marksam.Controls.NumberBox();
			this.chkMaxBaseCellsDeep = new System.Windows.Forms.CheckBox();
			this.numMaxPathsFromBaseCell = new Marksam.Controls.NumberBox();
			this.chkMaxPathsFromBaseCell = new System.Windows.Forms.CheckBox();
			this.numMaxNewBaseCells = new Marksam.Controls.NumberBox();
			this.chkMaxNewBaseCells = new System.Windows.Forms.CheckBox();
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tabPage4 = new System.Windows.Forms.TabPage();
			this.chkReportData = new System.Windows.Forms.CheckBox();
			this.chkEnableProbability = new System.Windows.Forms.CheckBox();
			this.numShowStepsDelay = new Marksam.Controls.NumberBox();
			this.chkShowSteps = new System.Windows.Forms.CheckBox();
			this.chkEnableSafeCell1 = new System.Windows.Forms.CheckBox();
			this.chkEnableFlagPath1 = new System.Windows.Forms.CheckBox();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.confoundsControl1 = new MineFlagger.ConfoundsControl();
			this.chkEnableSafeCell2 = new System.Windows.Forms.CheckBox();
			this.tabPage1 = new System.Windows.Forms.TabPage();
			this.butConfoundsSettings = new System.Windows.Forms.Button();
			this.chkEnableConfoundsExt = new System.Windows.Forms.CheckBox();
			this.tabControl1.SuspendLayout();
			this.tabPage4.SuspendLayout();
			this.tabPage2.SuspendLayout();
			this.tabPage1.SuspendLayout();
			this.SuspendLayout();
			// 
			// chkEnableFlagPath2
			// 
			this.chkEnableFlagPath2.Location = new System.Drawing.Point(14, 10);
			this.chkEnableFlagPath2.Name = "chkEnableFlagPath2";
			this.chkEnableFlagPath2.Size = new System.Drawing.Size(131, 24);
			this.chkEnableFlagPath2.TabIndex = 0;
			this.chkEnableFlagPath2.Text = "Enable Flag Path AI";
			this.chkEnableFlagPath2.CheckedChanged += new System.EventHandler(this.chkEnableFlagPath_CheckedChanged);
			// 
			// numMaxBaseCellsDeep
			// 
			this.numMaxBaseCellsDeep.Location = new System.Drawing.Point(172, 59);
			this.numMaxBaseCellsDeep.Name = "numMaxBaseCellsDeep";
			this.numMaxBaseCellsDeep.Size = new System.Drawing.Size(40, 20);
			this.numMaxBaseCellsDeep.TabIndex = 5;
			this.numMaxBaseCellsDeep.Text = "";
			this.numMaxBaseCellsDeep.TextChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// chkMaxBaseCellsDeep
			// 
			this.chkMaxBaseCellsDeep.Location = new System.Drawing.Point(14, 58);
			this.chkMaxBaseCellsDeep.Name = "chkMaxBaseCellsDeep";
			this.chkMaxBaseCellsDeep.Size = new System.Drawing.Size(156, 24);
			this.chkMaxBaseCellsDeep.TabIndex = 4;
			this.chkMaxBaseCellsDeep.Text = "Max  BaseCells deep:";
			this.chkMaxBaseCellsDeep.CheckedChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// numMaxPathsFromBaseCell
			// 
			this.numMaxPathsFromBaseCell.Location = new System.Drawing.Point(172, 83);
			this.numMaxPathsFromBaseCell.Name = "numMaxPathsFromBaseCell";
			this.numMaxPathsFromBaseCell.Size = new System.Drawing.Size(40, 20);
			this.numMaxPathsFromBaseCell.TabIndex = 7;
			this.numMaxPathsFromBaseCell.Text = "";
			this.numMaxPathsFromBaseCell.TextChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// chkMaxPathsFromBaseCell
			// 
			this.chkMaxPathsFromBaseCell.Location = new System.Drawing.Point(14, 82);
			this.chkMaxPathsFromBaseCell.Name = "chkMaxPathsFromBaseCell";
			this.chkMaxPathsFromBaseCell.Size = new System.Drawing.Size(156, 24);
			this.chkMaxPathsFromBaseCell.TabIndex = 6;
			this.chkMaxPathsFromBaseCell.Text = "Max Paths from BaseCell:";
			this.chkMaxPathsFromBaseCell.CheckedChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// numMaxNewBaseCells
			// 
			this.numMaxNewBaseCells.Location = new System.Drawing.Point(172, 107);
			this.numMaxNewBaseCells.Name = "numMaxNewBaseCells";
			this.numMaxNewBaseCells.Size = new System.Drawing.Size(40, 20);
			this.numMaxNewBaseCells.TabIndex = 9;
			this.numMaxNewBaseCells.Text = "";
			this.numMaxNewBaseCells.TextChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// chkMaxNewBaseCells
			// 
			this.chkMaxNewBaseCells.Location = new System.Drawing.Point(14, 106);
			this.chkMaxNewBaseCells.Name = "chkMaxNewBaseCells";
			this.chkMaxNewBaseCells.Size = new System.Drawing.Size(156, 24);
			this.chkMaxNewBaseCells.TabIndex = 8;
			this.chkMaxNewBaseCells.Text = "Max new BaseCells:";
			this.chkMaxNewBaseCells.CheckedChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.tabPage4,
																					  this.tabPage2,
																					  this.tabPage1});
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.Padding = new System.Drawing.Point(8, 3);
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(234, 187);
			this.tabControl1.TabIndex = 11;
			// 
			// tabPage4
			// 
			this.tabPage4.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.chkReportData,
																				   this.chkEnableProbability,
																				   this.numShowStepsDelay,
																				   this.chkShowSteps,
																				   this.chkEnableSafeCell1,
																				   this.chkEnableFlagPath1});
			this.tabPage4.Location = new System.Drawing.Point(4, 22);
			this.tabPage4.Name = "tabPage4";
			this.tabPage4.Size = new System.Drawing.Size(226, 161);
			this.tabPage4.TabIndex = 3;
			this.tabPage4.Text = "General";
			// 
			// chkReportData
			// 
			this.chkReportData.Location = new System.Drawing.Point(14, 106);
			this.chkReportData.Name = "chkReportData";
			this.chkReportData.Size = new System.Drawing.Size(131, 24);
			this.chkReportData.TabIndex = 16;
			this.chkReportData.Text = "Report data";
			this.chkReportData.CheckedChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// chkEnableProbability
			// 
			this.chkEnableProbability.Location = new System.Drawing.Point(14, 58);
			this.chkEnableProbability.Name = "chkEnableProbability";
			this.chkEnableProbability.Size = new System.Drawing.Size(131, 24);
			this.chkEnableProbability.TabIndex = 15;
			this.chkEnableProbability.Text = "Probability";
			this.chkEnableProbability.CheckedChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// numShowStepsDelay
			// 
			this.numShowStepsDelay.Location = new System.Drawing.Point(172, 83);
			this.numShowStepsDelay.Name = "numShowStepsDelay";
			this.numShowStepsDelay.Size = new System.Drawing.Size(40, 20);
			this.numShowStepsDelay.TabIndex = 14;
			this.numShowStepsDelay.Text = "";
			this.numShowStepsDelay.TextChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// chkShowSteps
			// 
			this.chkShowSteps.Location = new System.Drawing.Point(14, 82);
			this.chkShowSteps.Name = "chkShowSteps";
			this.chkShowSteps.Size = new System.Drawing.Size(157, 24);
			this.chkShowSteps.TabIndex = 13;
			this.chkShowSteps.Text = "Show steps; delay (ms):";
			this.chkShowSteps.CheckedChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// chkEnableSafeCell1
			// 
			this.chkEnableSafeCell1.Location = new System.Drawing.Point(14, 10);
			this.chkEnableSafeCell1.Name = "chkEnableSafeCell1";
			this.chkEnableSafeCell1.Size = new System.Drawing.Size(131, 24);
			this.chkEnableSafeCell1.TabIndex = 12;
			this.chkEnableSafeCell1.Text = "Safe Cell";
			this.chkEnableSafeCell1.CheckedChanged += new System.EventHandler(this.chkEnableSafeCell_CheckedChanged);
			// 
			// chkEnableFlagPath1
			// 
			this.chkEnableFlagPath1.Location = new System.Drawing.Point(14, 34);
			this.chkEnableFlagPath1.Name = "chkEnableFlagPath1";
			this.chkEnableFlagPath1.Size = new System.Drawing.Size(131, 24);
			this.chkEnableFlagPath1.TabIndex = 1;
			this.chkEnableFlagPath1.Text = "Flag Path";
			this.chkEnableFlagPath1.CheckedChanged += new System.EventHandler(this.chkEnableFlagPath_CheckedChanged);
			// 
			// tabPage2
			// 
			this.tabPage2.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.confoundsControl1,
																				   this.chkEnableSafeCell2});
			this.tabPage2.Location = new System.Drawing.Point(4, 22);
			this.tabPage2.Name = "tabPage2";
			this.tabPage2.Size = new System.Drawing.Size(226, 161);
			this.tabPage2.TabIndex = 1;
			this.tabPage2.Text = "Safe Cell";
			// 
			// confoundsControl1
			// 
			this.confoundsControl1.Location = new System.Drawing.Point(13, 38);
			this.confoundsControl1.Name = "confoundsControl1";
			this.confoundsControl1.Size = new System.Drawing.Size(202, 67);
			this.confoundsControl1.TabIndex = 11;
			// 
			// chkEnableSafeCell2
			// 
			this.chkEnableSafeCell2.Location = new System.Drawing.Point(14, 10);
			this.chkEnableSafeCell2.Name = "chkEnableSafeCell2";
			this.chkEnableSafeCell2.Size = new System.Drawing.Size(131, 24);
			this.chkEnableSafeCell2.TabIndex = 10;
			this.chkEnableSafeCell2.Text = "Enable Safe Cell AI";
			this.chkEnableSafeCell2.CheckedChanged += new System.EventHandler(this.chkEnableSafeCell_CheckedChanged);
			// 
			// tabPage1
			// 
			this.tabPage1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				   this.butConfoundsSettings,
																				   this.chkEnableConfoundsExt,
																				   this.chkEnableFlagPath2,
																				   this.chkMaxBaseCellsDeep,
																				   this.numMaxNewBaseCells,
																				   this.numMaxPathsFromBaseCell,
																				   this.chkMaxPathsFromBaseCell,
																				   this.numMaxBaseCellsDeep,
																				   this.chkMaxNewBaseCells});
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(226, 161);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "Flag Path";
			// 
			// butConfoundsSettings
			// 
			this.butConfoundsSettings.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.butConfoundsSettings.Location = new System.Drawing.Point(154, 35);
			this.butConfoundsSettings.Name = "butConfoundsSettings";
			this.butConfoundsSettings.Size = new System.Drawing.Size(58, 20);
			this.butConfoundsSettings.TabIndex = 11;
			this.butConfoundsSettings.Text = "Settings";
			this.butConfoundsSettings.Click += new System.EventHandler(this.butGoToConfounds_Click);
			// 
			// chkEnableConfoundsExt
			// 
			this.chkEnableConfoundsExt.Location = new System.Drawing.Point(14, 34);
			this.chkEnableConfoundsExt.Name = "chkEnableConfoundsExt";
			this.chkEnableConfoundsExt.Size = new System.Drawing.Size(142, 24);
			this.chkEnableConfoundsExt.TabIndex = 10;
			this.chkEnableConfoundsExt.Text = "Enable Confounds ext.";
			this.chkEnableConfoundsExt.CheckedChanged += new System.EventHandler(this.CheckedChanged);
			// 
			// AIConfigControl
			// 
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.tabControl1});
			this.Name = "AIConfigControl";
			this.Size = new System.Drawing.Size(235, 188);
			this.tabControl1.ResumeLayout(false);
			this.tabPage4.ResumeLayout(false);
			this.tabPage2.ResumeLayout(false);
			this.tabPage1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		#endregion


		// To presive value even if checkbox is unchecked
		string recursionLimit = "";
		private void butGoToConfounds_Click(object sender, System.EventArgs e) {
			ConfoundsDialog confoundsDialog = new ConfoundsDialog("Counfounds ext.", _rcConfigForFPConfoundsExt);
			if (_rcConfigForFPConfoundsExt.RecursionLimit == -1) {
				confoundsDialog.numRecursionLimit = recursionLimit;
			}
			
			confoundsDialog.ShowDialog();
			
			recursionLimit = confoundsDialog.numRecursionLimit;
			confoundsDialog.Dispose();
		}



	}

}
